<?
  /* File:    lib/xml/xml2array-xml_getchildren.php:
   * Type:    user interface
   * Author:  udo.schroeter@gmail.com
   * License: commercially licensed as part of the CMS package
   * Todo:    - move back into xml2array
   * Changes: -
   */

function _xml_GetChildren($vals, &$i)
{
  $children = array();     // Contains node data

  $marker_value = '$';
  $marker_att = '@';

  /* Node has CDATA before it's children */
  if (isset($vals[$i]['value']))
    $children[$marker_value] = $vals[$i]['value'];

  /* Loop through children */
  while (++$i < count($vals))
  {
    if (isset($vals[$i]['type']))
    {
      switch ($vals[$i]['type'])
      {
        /* Node has CDATA after one of it's children
          (Add to cdata found before if this is the case) */
        case 'cdata':
          if (isset($children[$marker_value]))
            $children[$marker_value] .= $vals[$i]['value'];
          else
            $children[$marker_value] = $vals[$i]['value'];
          break;
        /* At end of current branch */
        case 'complete':
          if (isset($vals[$i]['attributes'])) {
            $children[$vals[$i]['tag']][][$marker_att] = $vals[$i]['attributes'];
            $index = count($children[$vals[$i]['tag']])-1;

            if (isset($vals[$i]['value']))
            {
              $children[$vals[$i]['tag']][$index][$marker_value] =
                $vals[$i]['value'];
              // this is written for backward compatibility to a bug
              // (FIXME)
              $children[$vals[$i]['tag']][$index][] =
                $vals[$i]['value'];
            }
            else
              $children[$vals[$i]['tag']][$index][$marker_value] = '';
          }
          else
          {
            if (isset($vals[$i]['value']))
              $children[$vals[$i]['tag']][][$marker_value] = $vals[$i]['value'];
            else
              $children[$vals[$i]['tag']][][$marker_value] = '';
                  }
          break;
        /* Node has more children */
        case 'open':
          if (isset($vals[$i]['attributes'])) {
            $children[$vals[$i]['tag']][][$marker_att] =
              $vals[$i]['attributes'];

            $index = count($children[$vals[$i]['tag']])-1;
            $newChildren = _xml_GetChildren($vals, $i);
            if (!is_array($newChildren)) $newChildren = array($newChildren);
            $children[$vals[$i]['tag']][$index] =
              array_merge(getDefault($children[$vals[$i]['tag']][$index], array()), $newChildren);
          }
           else
          {
            $children[$vals[$i]['tag']][] = _xml_GetChildren($vals, $i);
          }
          break;
        /* End of node, return collected data */
        case 'close':
          return $children;
      }
    }
  }
}

?>
